/*******************************************************************************
 * Copyright 2012 Chao Zhang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.jbiolib.ncbi.genpept;

import org.jbiolib.ncbi.genbank.NCBIGenPept;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.jbiolib.io.FileWrite;
import org.jbiolib.ncbi.NCBIReference;

/**
 * @author Chao Zhang
 * @version 1.0 2012-11-06
 */

public class NCBIGenPeptPaser {

    public NCBIGenPeptPaser() {
    }
    
    public void loadMultiGenPeptFile(String inputFilePath){
        ArrayList tempDataArray = new ArrayList();
        NCBIGenPept loadingResult = new NCBIGenPept();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/hpbase2";
            Connection con = DriverManager.getConnection(url,"root", "");
            Statement stmt = con.createStatement();

            int index = 0;
            BufferedReader in = new BufferedReader(new FileReader(inputFilePath));
            String line = "";
            do {
                line = in.readLine();
                if(line != null) {
                    String tag = line.substring(0, (line.length()>=12?12:line.length())).trim();
                    if(tag.equals("LOCUS")) {
                        index++;
                        tempDataArray = new ArrayList();
                        tempDataArray.add(line);
                    } else if (tag.equals("//")) {
                        tempDataArray.add(line);
                        Object[] arr = tempDataArray.toArray();
                        String[] dataArr = new String[arr.length];
                        for(int i=0;i<arr.length;i++) {
                            dataArr[i]=arr[i].toString();
                        }
                        loadingResult = this.loadGenPeptFile(dataArr);
                        writeGenPeptFileIntoDB(loadingResult, stmt);
                    } else {
                        tempDataArray.add(line);
                    }
                }
            } while(line != null);
            con.close();
            in.close();
        } catch (Exception e) {
            System.out.println(loadingResult.getPlocus()+" has some problem");
            e.printStackTrace();
        }
    }
    /*
    public NCBIGenBank loadOneGenBankFile(String inputFilePath) {
        NCBIGenBank loadingResult = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/hpbase1";
            Connection con = DriverManager.getConnection(url,"root", "");
            Statement stmt = con.createStatement();
            String[] fileDetail = FileRead.readFile(inputFilePath, new String[0]);
            loadingResult = this.loadGenBankFile(fileDetail);
            writeGenBankFileIntoDB(loadingResult, stmt);
            con.close();
        }catch( Exception e ) {
            e.printStackTrace();
        }
        return loadingResult;
    }
    */
    public boolean writeGenPeptFileIntoDB(NCBIGenPept loadingResult, Statement stmt) {
        boolean sucTag = true;
        try {
            ArrayList<String> mainSql = new ArrayList();
            mainSql.add("INSERT INTO protein (plocus, locus, length, modifydate, definition, accession, version, gi, dbsource, keywords, source, organism, organtaxonomy, origin, " +
                    "isolate, isolation_source, host, map, strain, db_xref, note, country, clone_lib, clone, plasmid, PCR_primers, identified_by, disease, uploader, secStr) VALUES('"+
                    loadingResult.getPlocus()+"','"+loadingResult.getLocus()+"',"+loadingResult.getLength()+",'"+replacePrime(loadingResult.getModifydate())+"','"+replacePrime(loadingResult.getDefinition())+"','"+
                    replacePrime(loadingResult.getAccession())+"','"+replacePrime(loadingResult.getVersion())+"',"+replacePrime(loadingResult.getGi())+",'"+replacePrime(loadingResult.getDbsource())+"','"+
                    replacePrime(loadingResult.getKeywords())+"','"+replacePrime(loadingResult.getSource())+"','"+replacePrime(loadingResult.getOrganism())+"','"+replacePrime(loadingResult.getOrganTaxonomy())+"','"+
                    loadingResult.getOrgin()+"','"+replacePrime(loadingResult.getFeaturesSource().getIsolate())+"','"+replacePrime(loadingResult.getFeaturesSource().getIsolation_source())+"','"+
                    replacePrime(loadingResult.getFeaturesSource().getHost())+"','"+replacePrime(loadingResult.getFeaturesSource().getMap())+"','"+replacePrime(loadingResult.getFeaturesSource().getStrain())+"','"+
                    replacePrime(loadingResult.getFeaturesSource().getDb_xref())+"','"+replacePrime(loadingResult.getFeaturesSource().getNote())+"','"+replacePrime(loadingResult.getFeaturesSource().getCountry())+"','"+
                    replacePrime(loadingResult.getFeaturesSource().getClone_lib())+"','"+replacePrime(loadingResult.getFeaturesSource().getClone())+"','"+replacePrime(loadingResult.getFeaturesSource().getPlasmid())+"','"+
                    replacePrime(loadingResult.getFeaturesSource().getPCR_primers())+"','"+replacePrime(loadingResult.getFeaturesSource().getIdentified_by())+"','','Admin','');\n");
            for(int j=0; j<loadingResult.getReferenceList().size(); j++) {
                NCBIReference tempRef = loadingResult.getReferenceList().get(j);
                ResultSet tempResult = stmt.executeQuery("SELECT ref_index FROM reference where pubmed = "+tempRef.getPubmed()+" and title='"+replacePrime(tempRef.getTitle())+"' and journal='"+replacePrime(tempRef.getJournal())+"'");
                if (tempResult.next()) {
                    mainSql.add("INSERT INTO protein_reference (plocus, ref_index, ref_num, ref_des) VALUES('"+loadingResult.getPlocus()+"', "+tempResult.getString(1)+","+tempRef.getNumber()+",'"+tempRef.getDetail()+"');\n");
                } else {
                    mainSql.add("INSERT INTO reference (pubmed, authors, title, journal, remark) VALUES("+tempRef.getPubmed()+",'"+replacePrime(tempRef.getAuthors())+"','"+replacePrime(tempRef.getTitle())+"','"+replacePrime(tempRef.getJournal())
                            +"','"+replacePrime(tempRef.getRemark())+"');\n");
                    mainSql.add("INSERT INTO protein_reference (plocus, ref_index, ref_num, ref_des) VALUES('"+loadingResult.getPlocus()+"', LAST_INSERT_ID(),"+tempRef.getNumber()+",'"+tempRef.getDetail()+"');\n");
                }
            }
            mainSql.add("INSERT INTO protein_protein (plocus, protein_range, gene, locus_tag, name, gene_synonym, product, EC_number, note, UniProtKB_evidence, calculated_mol_wt, function, inference) VALUES('"+
                    loadingResult.getPlocus()+"','"+replacePrime(loadingResult.getFeaturesProtein().getProteinRange())+"','"+replacePrime(loadingResult.getFeaturesProtein().getGene())+
                    "','"+replacePrime(loadingResult.getFeaturesProtein().getLocus_tag())+"','"+replacePrime(loadingResult.getFeaturesProtein().getName())+"','"+replacePrime(loadingResult.getFeaturesProtein().getGene_synonym())+
                    "','"+replacePrime(loadingResult.getFeaturesProtein().getProduct())+"','"+replacePrime(loadingResult.getFeaturesProtein().getEC_number())+"','"+replacePrime(loadingResult.getFeaturesProtein().getNote())+
                    "','"+replacePrime(loadingResult.getFeaturesProtein().getUniProtKB_evidence())+"',"+loadingResult.getFeaturesProtein().getCalculated_mol_wt()+",'"+replacePrime(loadingResult.getFeaturesProtein().getFunction())+
                    "','"+replacePrime(loadingResult.getFeaturesProtein().getInference())+"');\n");
            mainSql.add("INSERT INTO protein_cds (plocus, CDS_range, gene, locus_tag, gene_synonym, coded_by, inference, note, transl_table, db_xref, codon_start, experiment, allele) VALUES('"+
                    loadingResult.getPlocus()+"','"+replacePrime(loadingResult.getFeaturesCDS().getCDSRange())+"','"+replacePrime(loadingResult.getFeaturesCDS().getGene())+
                    "','"+replacePrime(loadingResult.getFeaturesCDS().getLocus_tag())+"','"+replacePrime(loadingResult.getFeaturesCDS().getGene_synonym())+"','"+replacePrime(loadingResult.getFeaturesCDS().getCoded_by())+
                    "','"+replacePrime(loadingResult.getFeaturesCDS().getInference())+"','"+replacePrime(loadingResult.getFeaturesCDS().getNote())+"',"+loadingResult.getFeaturesCDS().getTransl_table()+
                    ",'"+replacePrime(loadingResult.getFeaturesCDS().getDb_xref())+"',"+loadingResult.getFeaturesCDS().getCodon_start()+",'"+replacePrime(loadingResult.getFeaturesCDS().getExperiment())+
                    "','"+replacePrime(loadingResult.getFeaturesCDS().getAllele())+"');\n");
            for(int j=0; j<loadingResult.getFeaturesRegion().size(); j++) {
                NCBIGenPeptFeaturesRegion tempRgeion = loadingResult.getFeaturesRegion().get(j);
                mainSql.add("INSERT INTO protein_region (plocus, region_index, region_range, region_name, note, db_xref, locus_tag, FTId, inference, gene, experiment, gene_synonym) VALUES('"+
                        loadingResult.getPlocus()+"',"+tempRgeion.getRegion_index()+",'"+replacePrime(tempRgeion.getRegionRange())+"','"+replacePrime(tempRgeion.getRegion_name())+"','"+replacePrime(tempRgeion.getNote())+"','"+
                        replacePrime(tempRgeion.getDb_xref())+"','"+replacePrime(tempRgeion.getLocus_tag())+"','"+replacePrime(tempRgeion.getFTId())+"','"+replacePrime(tempRgeion.getInference())+"','"+
                        replacePrime(tempRgeion.getExperiment())+"','"+replacePrime(tempRgeion.getGene())+"','"+replacePrime(tempRgeion.getGene_synonym())+"');\n");
            }
            for(int j=0; j<loadingResult.getFeaturesSite().size(); j++) {
                NCBIGenPeptFeaturesSite tempSite = loadingResult.getFeaturesSite().get(j);
                mainSql.add("INSERT INTO protein_site (plocus, site_index, site_range, gene, locus_tag, site_type, note, db_xref, inference, experiment) VALUES('"+
                        loadingResult.getPlocus()+"',"+tempSite.getSite_index()+",'"+replacePrime(tempSite.getSiteRange())+"','"+replacePrime(tempSite.getGene())+"','"+replacePrime(tempSite.getLocus_tag())+"','"+
                        replacePrime(tempSite.getSite_type())+"','"+replacePrime(tempSite.getNote())+"','"+replacePrime(tempSite.getDb_xref())+"','"+replacePrime(tempSite.getInference())+"','"+
                        replacePrime(tempSite.getExperiment())+"');\n");
            }
            for(int j=0;j<mainSql.size();j++) {
                stmt.executeUpdate(mainSql.get(j).toString());
                //System.out.println(mainSql.get(j).toString());
            }
        } catch (Exception e) {
            System.out.println(loadingResult.getPlocus()+" has some problem");
            sucTag = false;
            e.printStackTrace();
        }
        return sucTag;
    }

    public NCBIGenPept loadGenPeptFile(String[] fileDetail) {
        NCBIGenPept loadingResult = new NCBIGenPept();
        int i = 0;
        boolean printTag = true;
        try{
            int region_index = 0;
            int site_index = 0;
            ArrayList<NCBIReference> referenceList = new ArrayList<NCBIReference>();
            NCBIGenPeptFeaturesSource featuresSource;
            NCBIGenPeptFeaturesGene featureGene;
            NCBIGenPeptFeaturesProtein featuresProtein;
            NCBIGenPeptFeaturesCDS featuresCDS;
            ArrayList<NCBIGenPeptFeaturesRegion> featuresRegion = new ArrayList<NCBIGenPeptFeaturesRegion>();
            ArrayList<NCBIGenPeptFeaturesSite> featuresSite = new ArrayList<NCBIGenPeptFeaturesSite>();
            while(i<fileDetail.length) {
                String tag = fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim();
                if(tag.equals("LOCUS")) {
                    loadingResult.setPlocus(fileDetail[i].substring(12, 28).trim());
                    loadingResult.setLength(Integer.parseInt(fileDetail[i].substring(29, 40).trim()));
                    String modifyDate=fileDetail[i].substring(68, 79).trim();
                    SimpleDateFormat oldFormat = new SimpleDateFormat("dd-MMM-yyyy");
                    SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
                    modifyDate=newFormat.format(oldFormat.parse(modifyDate));
                    loadingResult.setModifydate(modifyDate);
                } else if(tag.equals("DEFINITION")) {
                    //loadingResult.setDefinition(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                    String definition = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                        definition += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                        i++;
                    }
                    i--;
                    loadingResult.setDefinition(definition);
                } else if(tag.equals("DBSOURCE")) {
                    //loadingResult.setDefinition(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                    String dbsource = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                    if(dbsource.indexOf("pdb")==-1) {
                        int startIndex = dbsource.indexOf("accession")+9;
                        String tempSubString = dbsource.substring(startIndex, dbsource.length());
                        if(tempSubString.indexOf(".")!=-1){
                            loadingResult.setLocus(tempSubString.substring(0,tempSubString.indexOf(".")).trim());
                        } else {
                            if(tempSubString.indexOf(";")!=-1){
                                loadingResult.setLocus(tempSubString.substring(0,tempSubString.indexOf(";")).trim());
                            } else {
                                loadingResult.setLocus(tempSubString.trim());
                            }
                        }
                    }
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                        dbsource += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                        i++;
                    }
                    i--;
                    loadingResult.setDbsource(dbsource);
                } else if(tag.equals("COMMENT")) {
                    //loadingResult.setDefinition(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                    String comment = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                        comment += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                        i++;
                    }
                    i--;
                    loadingResult.setComment(comment);
                } else if(tag.equals("ACCESSION")) {
                    loadingResult.setAccession(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                } else if(tag.equals("VERSION")) {
                    loadingResult.setVersion(fileDetail[i].substring(12, fileDetail[i].indexOf("GI:")).trim());
                    loadingResult.setGi(fileDetail[i].substring(fileDetail[i].indexOf("GI:")+3, fileDetail[i].length()).trim());
                } else if(tag.equals("KEYWORDS")) {
                    String keywords = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                        keywords+=fileDetail[i].trim();
                        i++;
                    }
                    i--;
                    loadingResult.setKeywords(keywords);
                } else if(tag.equals("SOURCE")) {
                    loadingResult.setSource(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                } else if(tag.equals("ORGANISM")) {
                    loadingResult.setOrganism(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                    String organText = "";
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                        organText+=fileDetail[i].trim();
                        i++;
                    }
                    i--;
                    loadingResult.setOrganTaxonomy(organText);
                } else if(tag.equals("DBLINK")) {
                    loadingResult.setDblink(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                } else if(tag.equals("REFERENCE")) {
                    int refNum = 0;
                    String refDetail = "";
                    String authors = "";
                    String title = "";
                    String journal = "";
                    String remark = "";
                    int pubmed = 0;
                    if(fileDetail[i].indexOf("(")!=-1){
                        refNum = Integer.parseInt(fileDetail[i].substring(12, fileDetail[i].indexOf("(")).trim());
                        refDetail = fileDetail[i].substring(fileDetail[i].indexOf("("), fileDetail[i].length()).trim();
                    } else {
                        refNum = Integer.parseInt(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                        refDetail = "";
                    }
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=2?2:fileDetail[i].length())).trim().equals("")){
                        String tempTag = fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim();
                        if(tempTag.equals("AUTHORS")) {
                            authors += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                authors += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("TITLE")) {
                            title += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                title += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("JOURNAL")) {
                            journal += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                journal += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("PUBMED")) {
                            pubmed = Integer.parseInt(fileDetail[i].substring(12, fileDetail[i].length()).trim());
                        } else if(tempTag.equals("CONSRTM")) {
                            
                        } else if(tempTag.equals("REMARK")) {
                            remark += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                remark += fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                i++;
                            }
                            i--;
                        } else {
                            if(printTag==true)
                                FileWrite.writeInput(loadingResult.getPlocus()+":Refe\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                //System.out.println(loadingResult.getPlocus()+"\t"+i+"\t"+fileDetail[i]);
                        }
                        i++;
                    }
                    i--;
                    //System.out.println(loadingResult.getPlocus()+"\t"+i+"\t"+fileDetail[i]);
                    referenceList.add(new NCBIReference(refNum, refDetail, authors, title, journal, pubmed, remark));
                } else if(tag.equals("FEATURES")) {
                    String source_sourceRange = "";
                    String source_organism = "";
                    String source_isolate = "";
                    String source_isolation_source = "";
                    String source_host = "";
                    String source_map = "";
                    String source_strain = "";
                    String source_db_xref = "";
                    String source_note = "";
                    String source_country = "";
                    String source_clone_lib = "";
                    String source_clone = "";
                    String source_plasmid = "";
                    String source_PCR_primers = "";
                    String source_identified_by = "";

                    String gene_geneRange = "";
                    String gene_gene = "";
                    String gene_locus_tag = "";
                    String gene_gene_synonym = "";

                    String protein_proteinRange = "";
                    String protein_gene = "";
                    String protein_locus_tag = "";
                    String protein_name = "";
                    String protein_gene_synonym = "";
                    String protein_product = "";
                    String protein_EC_number = "";
                    String protein_note = "";
                    String protein_UniProtKB_evidence = "";
                    int protein_calculated_mol_wt = 0;
                    String protein_function = "";
                    String protein_inference = "";

                    String CDS_CDSRange = "";
                    String CDS_gene = "";
                    String CDS_locus_tag = "";
                    String CDS_gene_synonym = "";
                    String CDS_coded_by = "";
                    String CDS_inference = "";
                    String CDS_note = "";
                    int CDS_transl_table = 0;
                    String CDS_db_xref = "";
                    int CDS_codon_start = 0;
                    String CDS_experiment = "";
                    String CDS_allele = "";
                    
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=2?2:fileDetail[i].length())).trim().equals("")){
                        String tempTag = fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim();
                        if(tempTag.equals("source")) {
                            source_sourceRange = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/organism")!=-1) {
                                    source_organism = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/db_xref")!=-1) {
                                    source_db_xref += tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim() + "@";
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/host")!=-1) {
                                    source_host = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        source_host += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(source_host.lastIndexOf("\"")!=-1){
                                        source_host = source_host.substring(0, source_host.lastIndexOf("\""));
                                    }
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/map")!=-1) {
                                    source_map = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        source_map += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(source_map.lastIndexOf("\"")!=-1){
                                        source_map = source_map.substring(0, source_map.lastIndexOf("\""));
                                    }
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/strain")!=-1) {
                                    source_strain = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/isolation_source")!=-1) {
                                    source_isolation_source = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        source_isolation_source += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(source_isolation_source.lastIndexOf("\"")!=-1){
                                        source_isolation_source = source_isolation_source.substring(0, source_isolation_source.lastIndexOf("\""));
                                    }//System.out.println(CDS_name);
                                } else if(tempString.indexOf("/isolate")!=-1) {
                                    source_isolate = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/identified_by")!=-1) {
                                    source_identified_by = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/note")!=-1) {
                                    source_note = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        source_note += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(source_note.lastIndexOf("\"")!=-1){
                                        source_note = source_note.substring(0, source_note.lastIndexOf("\""));
                                    }
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/PCR_primers")!=-1) {
                                    source_PCR_primers = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        source_PCR_primers += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(source_PCR_primers.lastIndexOf("\"")!=-1){
                                        source_PCR_primers = source_PCR_primers.substring(0, source_PCR_primers.lastIndexOf("\""));
                                    }
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/country")!=-1) {
                                    source_country = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        source_country += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(source_country.lastIndexOf("\"")!=-1){
                                        source_country = source_country.substring(0, source_country.lastIndexOf("\""));
                                    }
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/clone_lib")!=-1) {
                                    source_clone_lib = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        source_clone_lib += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(source_clone_lib.lastIndexOf("\"")!=-1){
                                        source_clone_lib = source_clone_lib.substring(0, source_clone_lib.lastIndexOf("\""));
                                    }
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/clone")!=-1) {
                                    source_clone = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/plasmid")!=-1) {
                                    source_plasmid = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();                                    //System.out.println(gene_db_xref);
                                } else {
                                    if(printTag==true)
                                        FileWrite.writeInput(loadingResult.getPlocus()+":Sour\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                        //System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                            //System.out.println(features_source);
                        } else if(tempTag.equals("gene")) {
                            gene_geneRange = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            //System.out.println(i+"\t"+gene_geneRange);
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/gene")!=-1) {
                                    gene_gene = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/locus_tag")!=-1) {
                                    gene_locus_tag = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/gene_synonym")!=-1) {
                                    gene_gene_synonym = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else {
                                    if(printTag==true)
                                        FileWrite.writeInput(loadingResult.getPlocus()+":Gene\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                        //System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("Protein")) {
                            protein_proteinRange = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/gene=")!=-1) {
                                    protein_gene = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/locus_tag=")!=-1) {
                                    protein_locus_tag = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/name=")!=-1) {
                                    protein_name = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        protein_name += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(protein_name.lastIndexOf("\"")!=-1){
                                        protein_name = protein_name.substring(0, protein_name.lastIndexOf("\""));
                                    }
                                    //System.out.println(note);
                                } else if(tempString.indexOf("/gene_synonym=")!=-1) {
                                    protein_gene_synonym = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/EC_number=")!=-1) {
                                    protein_EC_number = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/note=")!=-1) {
                                    protein_note = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        protein_note += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(protein_note.lastIndexOf("\"")!=-1){
                                        protein_note = protein_note.substring(0, protein_note.lastIndexOf("\""));
                                    }
                                    //System.out.println(note);
                                } else if(tempString.indexOf("/product=")!=-1) {
                                    protein_product = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        protein_product += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(protein_product.lastIndexOf("\"")!=-1){
                                        protein_product = protein_product.substring(0, protein_product.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/function=")!=-1) {
                                    protein_function = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        protein_function += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(protein_function.lastIndexOf("\"")!=-1){
                                        protein_function = protein_function.substring(0, protein_function.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/UniProtKB_evidence=")!=-1) {
                                    protein_UniProtKB_evidence = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/calculated_mol_wt=")!=-1) {
                                    protein_calculated_mol_wt = Integer.parseInt(tempString.substring(tempString.indexOf("=")+1, tempString.length()).trim());
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/inference=")!=-1) {
                                    protein_inference = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        protein_inference += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(protein_inference.lastIndexOf("\"")!=-1){
                                        protein_inference = protein_inference.substring(0, protein_inference.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else {
                                    if(printTag==true)
                                        FileWrite.writeInput(loadingResult.getPlocus()+":Prot\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                        //System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("CDS")) {
                            CDS_CDSRange = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/gene=")!=-1) {
                                    CDS_gene = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/locus_tag=")!=-1) {
                                    CDS_locus_tag = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/allele=")!=-1) {
                                    CDS_allele = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/gene_synonym=")!=-1) {
                                    CDS_gene_synonym = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/coded_by=")!=-1) {
                                    CDS_coded_by = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_coded_by += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_coded_by.lastIndexOf("\"")!=-1){
                                        CDS_coded_by = CDS_coded_by.substring(0, CDS_coded_by.lastIndexOf("\""));
                                    }//System.out.println(CDS_name);
                                } else if(tempString.indexOf("/inference")!=-1) {
                                    CDS_inference = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_inference += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_inference.lastIndexOf("\"")!=-1){
                                        CDS_inference = CDS_inference.substring(0, CDS_inference.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/note")!=-1) {
                                    CDS_note = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_note += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_note.lastIndexOf("\"")!=-1){
                                        CDS_note = CDS_note.substring(0, CDS_note.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/transl_table")!=-1) {
                                    CDS_transl_table = Integer.parseInt(tempString.substring(tempString.indexOf("=")+1, tempString.length()).trim());
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/db_xref")!=-1) {
                                    CDS_db_xref += tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim() + "@";
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/experiment")!=-1) {
                                    CDS_experiment = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        CDS_experiment += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(CDS_experiment.lastIndexOf("\"")!=-1){
                                        CDS_experiment = CDS_experiment.substring(0, CDS_experiment.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/codon_start")!=-1) {
                                    CDS_codon_start = Integer.parseInt(tempString.substring(tempString.indexOf("=")+1, tempString.length()).trim());
                                    //System.out.println(CDS_name);
                                } else {
                                    if(printTag==true)
                                        FileWrite.writeInput(loadingResult.getPlocus()+":CDS\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                        //System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                        } else if(tempTag.equals("Region")) {
                            region_index++;
                            String regionRange = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            String region_name = "";
                            String note = "";
                            String db_xref = "";
                            String locus_tag = "";
                            String FTId = "";
                            String inference = "";
                            String gene = "";
                            String experiment = "";
                            String gene_synonym = "";
                            i++;
                            //System.out.println(i+"\t"+fileDetail[i]);
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/gene")!=-1) {
                                    gene = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/locus_tag")!=-1) {
                                    locus_tag = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/region_name")!=-1) {
                                    region_name = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        region_name += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(region_name.lastIndexOf("\"")!=-1){
                                        region_name = region_name.substring(0, region_name.lastIndexOf("\""));
                                    }
                                    //System.out.println(note);
                                } else if(tempString.indexOf("/gene_synonym")!=-1) {
                                    gene_synonym = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/FTId")!=-1) {
                                    FTId = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/note")!=-1) {
                                    note = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        note += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(note.lastIndexOf("\"")!=-1){
                                        note = note.substring(0, note.lastIndexOf("\""));
                                    }
                                    //System.out.println(note);
                                } else if(tempString.indexOf("/inference")!=-1) {
                                    inference = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        inference += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(inference.lastIndexOf("\"")!=-1){
                                        inference = inference.substring(0, inference.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/experiment")!=-1) {
                                    experiment = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        experiment += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(experiment.lastIndexOf("\"")!=-1){
                                        experiment = experiment.substring(0, experiment.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/db_xref")!=-1) {
                                    db_xref += tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim() + "@";
                                    //System.out.println(gene_db_xref);
                                } else {
                                    if(printTag==true)
                                        FileWrite.writeInput(loadingResult.getPlocus()+":Regi\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                        //System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                            featuresRegion.add(new NCBIGenPeptFeaturesRegion(region_index, regionRange, region_name, note, db_xref, locus_tag, FTId, inference, gene, experiment, gene_synonym));
                        } else if(tempTag.equals("Site")) {
                            site_index++;
                            String siteRange = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            String note = "";
                            String db_xref = "";
                            String locus_tag = "";
                            String site_type = "";
                            String inference = "";
                            String gene = "";
                            String experiment = "";
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                siteRange += " " + fileDetail[i].trim();
                                i++;
                            }
                            //i--;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/gene")!=-1) {
                                    gene = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene);
                                } else if(tempString.indexOf("/locus_tag")!=-1) {
                                    locus_tag = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/site_type")!=-1) {
                                    site_type = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/note")!=-1) {
                                    note = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        note += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(note.lastIndexOf("\"")!=-1){
                                        note = note.substring(0, note.lastIndexOf("\""));
                                    }
                                    //System.out.println(note);
                                } else if(tempString.indexOf("/inference")!=-1) {
                                    inference = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        inference += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(inference.lastIndexOf("\"")!=-1){
                                        inference = inference.substring(0, inference.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else if(tempString.indexOf("/db_xref")!=-1) {
                                    db_xref += tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim() + "@";
                                    //System.out.println(gene_db_xref);
                                } else if(tempString.indexOf("/experiment")!=-1) {
                                    experiment = tempString.substring(tempString.indexOf("=")+2, tempString.length()).trim();
                                    i++;
                                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")&&fileDetail[i].length()>=22?(!fileDetail[i].substring(21,22).trim().equals("/")):false){
                                        experiment += " " + fileDetail[i].trim();
                                        i++;
                                    }
                                    i--;
                                    if(experiment.lastIndexOf("\"")!=-1){
                                        experiment = experiment.substring(0, experiment.lastIndexOf("\""));
                                    }
                                    //System.out.println(product);
                                } else {
                                    if(printTag==true)
                                        FileWrite.writeInput(loadingResult.getPlocus()+":Site\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                        //System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                            featuresSite.add(new NCBIGenPeptFeaturesSite(site_index, siteRange, gene, locus_tag, site_type, note, db_xref, inference, experiment));
                        } else if(tempTag.equals("SecStr")) {
                            String secStrRange = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                            String note = "";
                            String sec_str_type = "";
                            i++;
                            while(fileDetail[i].substring(0, (fileDetail[i].length()>=12?12:fileDetail[i].length())).trim().equals("")){
                                String tempString = fileDetail[i].substring(12, fileDetail[i].length()).trim();
                                if(tempString.indexOf("/note")!=-1) {
                                    note = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else if(tempString.indexOf("/sec_str_type")!=-1) {
                                    sec_str_type = tempString.substring(tempString.indexOf("=")+2, tempString.lastIndexOf("\"")).trim();
                                    //System.out.println(CDS_name);
                                } else {
                                    if(printTag==true)
                                        FileWrite.writeInput(loadingResult.getPlocus()+":Regi\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                        //System.out.println(i+"\t"+fileDetail[i]);
                                }
                                i++;
                            }
                            i--;
                        } else {
                            if(printTag==true)
                                FileWrite.writeInput(loadingResult.getPlocus()+":feat\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                                //System.out.println(i+"\t"+fileDetail[i]);
                        }
                        i++;
                    }
                    i--;
                    featuresSource = new NCBIGenPeptFeaturesSource(source_sourceRange, source_organism, source_isolate, source_isolation_source,
                            source_host, source_map, source_strain, source_db_xref, source_note, source_country, source_clone_lib, source_clone,
                            source_plasmid, source_PCR_primers, source_identified_by);
                    featureGene = new NCBIGenPeptFeaturesGene(gene_geneRange, gene_gene, gene_locus_tag, gene_gene_synonym);
                    featuresProtein = new NCBIGenPeptFeaturesProtein(protein_proteinRange, protein_gene, protein_locus_tag, protein_name, protein_gene_synonym,
                            protein_product, protein_EC_number, protein_note, protein_UniProtKB_evidence, protein_calculated_mol_wt, protein_function, protein_inference);
                    featuresCDS = new NCBIGenPeptFeaturesCDS(CDS_CDSRange, CDS_gene, CDS_locus_tag, CDS_gene_synonym, CDS_coded_by, CDS_inference, CDS_note,
                            CDS_transl_table, CDS_db_xref, CDS_codon_start, CDS_experiment, CDS_allele);
                    
                    loadingResult.setFeaturesSource(featuresSource);
                    loadingResult.setFeatureGene(featureGene);
                    loadingResult.setFeaturesProtein(featuresProtein);
                    loadingResult.setFeaturesCDS(featuresCDS);
                    loadingResult.setFeaturesRegion(featuresRegion);
                    loadingResult.setFeaturesSite(featuresSite);
                } else if(tag.equals("ORIGIN")) {
                    String sequence = fileDetail[i].substring(10, fileDetail[i].length()).trim();
                    i++;
                    while(fileDetail[i].substring(0, (fileDetail[i].length()>=2?2:fileDetail[i].length())).trim().equals("")){
                        sequence+= " "+fileDetail[i].substring(10, fileDetail[i].length()).trim();
                        i++;
                    }
                    i--;
                    loadingResult.setOrgin(sequence.trim());
                } else if(tag.equals("//")) {
                    break;
                } else {
                    if(printTag==true)
                        FileWrite.writeInput(loadingResult.getPlocus()+":othe\t"+i+"\t"+fileDetail[i], "C:\\Documents and Settings\\Administrator\\Desktop\\mydata\\otherprotein.txt");
                        //System.out.println(i+"\t"+fileDetail[i]);
                }
                i++;
            }
            loadingResult.setReferenceList(referenceList);
        }catch( Exception e ) {
            System.out.println(loadingResult.getLocus()+"\t"+i+"\t"+fileDetail[i]);
            e.printStackTrace();
        }

        return loadingResult;
    }

    private String replacePrime(String inputString) {
        return inputString.replace("'", "\\'");
    }
}
